<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends CommonController {
	public function indexAction(){
		$sql = "select t1.*,t3.title,t3.id as agid from user t1 left join auth_group_access t2 on t1.id=t2.uid left join auth_group t3 on t2.group_id = t3.id where t1.isadmin=1 group by id";

		// $sql = "select t2.username,t3.title from auth_group_access t1 left join user t2 on t1.uid=t2.id left join auth_group t3 on t1.group_id = t3.id where t2.isadmin=1";

		$res = M()->query($sql);
		var_dump($res);
		foreach ($res as $key => $value) {
			$res[$key]['reg_time'] = date('Y-m-d H:i:s',$res[$key]['reg_time']);
		}

		$grouplistsql = "select * from auth_group";

		$grouplistres = M()->query($grouplistsql);

		// var_dump($grouplistres);

		$count = count($res);

		$this->assign('res',$res);

		$this->assign('count',$count);

		$this->assign('grouplistres',$grouplistres);

		$this->display();
	}

	//添加
	public function addAction(){
		if (IS_POST) {
			var_dump($_POST);
			// 给user表里的用户授权管理员权限
			//获取当前填写的email，去user表判断这个用户是否存在，如果不存在，报错，存在，则获取这个用户的id，更新他的状态isadmin
			// 之后通过用户id，和post过来的权限组group id更新auth_group_access表
			$email = I('post.email','');
			$grouplist = I('post.grouplist','');
			// $id = I('post.id','');
			$sql = "select * from user where email='{$email}'";
			$res = M()->query($sql);
			if ($res) {
				$this->success('账户可用',U('admin/admin/index'));
			}else{
				$this->error('用户不存在',U('admin/admin/index'));
			}
			$id = current($res)['id'];
			$sqlupdate = "update user set isadmin =1 where id='{$id}'";
			$isadmin = M()->execute($sqlupdate);
			//插入数据库auth_group_access表
			$insertsql = "insert into auth_group_access(uid,group_id) values('{$id}','{$grouplist}')";
			$insertres = M()->execute($insertsql);
			if ($insertres) {
				$this->success('添加成功',U('admin/admin/index'));
			}else{
				$this->error('添加失败',U('admin/admin/index'));
			}
			// var_dump($insertsql);
		}
		// $this->display();
	}

	//编辑修改
	public function editAction(){
		if (IS_POST) {
			// var_dump($_POST);exit;
			$email = $_POST['email'];
			$title = $_POST['title'];
			$group_id = $_POST['group_id'];
    		$uid = $_POST['uid'];
    		// $groupsql = "select * from auth_group where id ='{$id}'";
    		$groupsql = "update auth_group_access set group_id='{$group_id}' where uid='{$uid}'";
    		$groupres = M()->execute($groupsql);
    		if ($groupres) {
    			$this->success('修改成功',U('admin/admin/index'));
    		}else{
    			$this->error('修改失败',U('admin/admin/index'));
    		}
		}else{
			$id = I('get.id','');
			if (empty($id)) {
				$this->error('非法请求',U('admin/admin/index'));
			}else{
				$sql = "select t1.id as agid,t1.title,t2.*,t3.username from auth_group t1 left join auth_group_access t2 on t1.id=t2.group_id left join user t3 on t2.uid=t3.id where t3.id='{$id}'";
				$res = current(M()->query($sql));
				$groupsql = "select * from auth_group";
				$groupres = M()->query($groupsql);		
				$this->assign('res',$res);
				$this->assign('groupres',$groupres);
			}
		}		
		$this->display();
	}

	//删除
	public function delAction(){
		$id = I('get.id');
		$sql1 = "update user set isadmin = 0 where id='{$id}'";
		$res1 = M()->execute($sql1);
		
        $sql = "delete from  auth_group_access where uid='{$id}'";

        $res = M()->execute($sql);

        if($res){
            $this->success('删除成功',U('admin/admin/index'));
        }else{
            $this->error('删除失败',U('admin/admin/index'));
        }
	}

}